﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Webknife.Data;
using System.IO;
using System.Drawing.Imaging;
using System.Threading;

namespace StuImg
{
    public partial class MainFrm : Form
    {
        string gname = "";
        string cname = "";
        public MainFrm()
        {
            InitializeComponent();
        }
        private void RenderImage(string filename,byte[] byteImg)
        {
            MemoryStream ms = new MemoryStream(byteImg);
            System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
            image.Save(filename);
        } 
        public void FolderCheck(string Folder)
        {
            DirectoryInfo info = new DirectoryInfo(Folder);
            if (!info.Exists){
                info.Create();
            }
        }


        public delegate void SetLogCallback(string outPut, ProgressBar parBar);

        private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            //生成全校图片
            string outPut = "";
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog(this) == DialogResult.OK){
                outPut = dialog.SelectedPath + "\\Template\\" + gname + cname;
                FolderCheck(outPut);
            }
             DoWork(outPut, progressBar1);

            MessageBox.Show("生成全校图片完成！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            button2.Enabled = true;
        }

        public void DoWork(string outPut, ProgressBar parBar)
        {
            string commandText = "select stuintid as 编号,stuNum as 学号,stu_name as 姓名,sex as 性别,PersonImage as 图片,imgtype as 图片类型 " +
                   " from Stu_Info where  PersonImage is not null";
            DataTable table = DbHelper.ExecuteDataset(SysConst.DB_NAME, commandText).Tables[0];
            parBar.Maximum = table.Rows.Count;
            parBar.Minimum = 0;
            int num = 0;
            foreach (DataRow row in table.Rows){
                string stuid = row["学号"].ToString().Trim();
                string stuname = chs2py.convert(row["姓名"].ToString().Trim());
                byte[] barrImage = (byte[])row["图片"];
                RenderImage(outPut + "\\" + stuid + ".jpg", barrImage);
                parBar.Value = num;
                num++;
            }
        }
    }
}
